<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NetworkController
 *
 * @author mateusz.ledzewicz
 */
class TestingController extends Controller {
    protected function begin() {
        $this->view = new ViewTesting();
        
    }
    
    protected function run() {
        
        $type = (isSet($_GET[ConfigInput::actionGet])) ? $_GET[ConfigInput::actionGet] : null;
        switch($type){
            case 'run':
                $this->resultRun();
            break;
            case 'test':
               $this->testRun();
            break;
            case 'results':
               $this->testResults();
            break;
            default:
                $this->infoRun();
            break; 
        }
    }

    protected function resultRun() {
        $id = (isSet($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : null;
        $result = '';
        $teached = Network_teached::loadModel($id);
        if(is_null($teached) || $teached->NewRecord)
        {
            throw new Exception('Network does\'t exist');
        }
        $prepared = Network_prepared::loadModel($teached->id_network_prepared, array('Network'));
       
        $structure = explode('-', $prepared->id_network->structure);
        
        
        if(count($_POST) > 0)
        {
            $result = $teached->getResult($_POST['input'])[0];
            
          
        }
        
        $this->view->setSpecialVar('networkIn', $structure[0]);
        $this->view->setVar('result', $result);
        $this->view->setVar('id', $id);
    }
    
    protected function testRun()
    {
        
        $id = (isSet($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : null;
        $result = '';
        $teached = Network_teached::loadModel($id);
        
        if(is_null($teached) || $teached->NewRecord)
        {
            throw new Exception('Network does\'t exist');
        }
        $trainingData = Training_data::loadModel($teached->id_training_file);
        $prepared = Network_prepared::loadModel($teached->id_network_prepared, array('Network'));
        $struct = explode('-', $prepared->id_network->structure );
        $number_in = $struct[0];
        $number_out= $struct[count($struct)-1];
        $this->view->setSpecialVar('testing_data', Test_data::getAll("where number_in = '{$number_in}' and number_out = '{$number_out}' and data_in='{$trainingData->data_in}' and data_out='{$trainingData->data_out}'"));
        $test = Test::loadModel();
       
        if(count($_POST) > 0)
        {
           
            $test->setValues($_POST);
            $test->id_network_teached = $teached->id_network_teached;
            
            $testData = Test_data::loadModel($test->id_test_data);
            if(is_null($testData) || $testData->NewRecord)
            {
                throw new Exception('Test data does\'t exist');
            }
            
            $params = BasicFunctions::prepareFileName($teached->name).' ';
            $params .= basename($testData->file).' ';
            $params .= 0; //MSE error
            copy($teached->file, basename($teached->file));
            copy($testData->file, basename($testData->file));
            exec(ConfigPath::network_test.' '.$params, $output);
          
            if($output[0] != '1')
            {
                throw new Exception('NETWORK_TEST failed');
            }
           
            $result = $output[1];
            
            unlink(basename($teached->file));
            unlink(basename($testData->file)); 
            $test->output = $result;
            $test->save();
        }
        $this->view->setVar('result', $result);
        $this->view->setVar('id', $id);
    }
    
    protected function infoRun()
    {
        $id = (isSet($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : null;
        $this->view->setModel(Test::loadModel(), 'header');
        $this->view->setSpecialVar('list', Test::getAll("where id_network_teached='$id'"));
        $this->view->setVar('id', $id);
    }
    
    protected function testResults()
    {
       $id = (isSet($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : null;
       $test = Test::loadModel($id, array('Test_data'));
       
       
       $chartData = new ChartData($test->id_test, $test->id_test_data->id_test_data, ChartData::DataTest);
       $chartData ->getData(2*$test->id_test_data->number);
       $this->view->setVar('chartDataHeader', "['sdsd', 'sdsdsad', 'sdsdsd'],");
       $this->view -> setVar('chartData', $chartData->getChartNetworkVSFile());
       
       
       
    }

    //put your code here
}

?>
